//
//  SettingViewController.m
//  B_Bycicle
//
//  Created by dllo on 16/4/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SettingViewController.h"
#import "OffLineMapViewController.h"
#import "HelpViewController.h"
#import "AboutViewController.h"
#define NOTIFICATION_CENTER [NSNotificationCenter defaultCenter]
#define USERDEFAULTS [NSUserDefaults standardUserDefaults]
@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableView;
@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    self.tableView.tableFooterView = [[UIView alloc]init];
    [self createTable];
    
}
- (void)createTable{
    self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString *str = @"通用";
    return str;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    if (1 == indexPath.row) {
        cell.accessoryType = UITableViewCellStyleDefault;
    }else{
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    if (0 == indexPath.row) {
        cell.textLabel.text = @"离线地图";
    }else if (1 == indexPath.row){
        cell.textLabel.text = @"夜间模式";
        UISwitch *night = [[UISwitch alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 60, 80, 0, 40)];
        [self.tableView addSubview:night];
        [night addTarget:self action:@selector(openNight:) forControlEvents:(UIControlEventValueChanged)];
        night.on = [USERDEFAULTS boolForKey:@"isNight"];
    }else if (2 == indexPath.row){
        cell.textLabel.text = @"清除缓存";
    }else if (3 == indexPath.row){
        cell.textLabel.text = @"帮助";
    }else{
        cell.textLabel.text = @"关于";
    }
    
    return cell;
}
//夜间
- (void)openNight:(UISwitch *)night
{
    BOOL isNight = night.isOn;
    if (isNight) {
        NSLog(@"进入夜间模式");
        [NOTIFICATION_CENTER postNotificationName:@"night" object:nil];
    } else {
        NSLog(@"进入日间模式");
        [NOTIFICATION_CENTER postNotificationName:@"day" object:nil];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (0 == indexPath.row) {
        OffLineMapViewController *OffLineMap = [[OffLineMapViewController alloc]init];
        [self.navigationController pushViewController:OffLineMap animated:YES];
    }else if (3 == indexPath.row){
        HelpViewController *help = [[HelpViewController alloc]init];
        [self.navigationController pushViewController:help animated:YES];
    }else if (4 == indexPath.row){
        AboutViewController *about = [[AboutViewController alloc]init];
        [self.navigationController pushViewController:about animated:YES];
    }else{
        NSLog(@"xi");
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
